package Gui;

import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import Common.CRes;
import Common.Camera;
import Common.Util;
import Entity.Bee;
import Entity.Line;
import Entity.Rec;
import Main.CCanvas;
import Main.CObject;
import Main.CScreen;
import Main.Map;


public class GameSreen extends CScreen {
	 
			private static final int PI = 0;
			public static int width,height;		
			public int point=0;
			public CObject ob= new CObject() {
						
			public void paint(Graphics g) {
					// TODO Auto-generated method stub
					
				}
			};
			//font 
			public static Font font1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
			public static Font font2 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
			public static int giay=60,miligiay=0, minutes;						

			Random r= new Random();
			// kiem tra qua man
			public  byte checkLevel=0;//kiemtraman=false: man 1, else man 2
			public byte checkTypeWalls=0;//kiem tra loai wall
			public static int diem;// diem gioi han tung man
			int t4;
			public static int a=0;
	
			byte[] data = new byte[500];
			byte[]  man= new byte[200];
			byte[] data_L;
			byte[] data_M;
			public	static boolean kt_savegame=false;
			public static final int WHITE = 0xFFFFFF;
			boolean startGame=false,gameOver=false,checkDisplayScore=false;
			int[] xx= {0,1,2,3,4,0,-1,-2,-3,-4};
			byte runCharStartGame=0;
			byte timeDisplayScore=0;
			public Map map;
			Rec rec1,rec2;
			Line line,line1;
			public Camera camera;
			public static Bee Bee_Player;
		
	public GameSreen()
	{
		Bee_Player = new Bee();
		map=new Map();			
		camera = new Camera();

	}
	
	//load all infor of tree to replace tile tree on map
	public static void loadTree(Vector x, Vector y, Vector type) {

	}
	
	//sort mot mang tu cao toi thap
	public static void sort(byte[] array) 
	{
		for (int i = 0; i < array.length; i+=4) 
		{
			int max = array[i]; // Lưu phần tử lớn nhất
			int index = i; // lưu vị trí chứa phần tử lớn nhất
			for (int j = i + 4; j < array.length; j+=4)
			{
				if(max < array[j]){
				max = array[j];
				index = j;
				}
			}
			// Nếu chỉ số đã thay đổi, ta sẽ hoán vị
			if(index != i)
			{
				int temp = array[i];
				array[i] = array[index];
				array[index] = (byte)temp;
			}
		}
	}

	//save game vao RMS
	public void saveGame() {
		
		 data_L = CRes.loadRMSData("BRData");
		 data_M=CRes.loadRMSData("BRData_man");
		
		if (data_L!=null && data_L.length>3)
		{
			sort(data_L);
		}		
	}
	
	// hàm random
	public  static  final  int  random(int  a, int  b)
	 {		
		 Random random = new  Random(System.currentTimeMillis());
		 while  (true)
		 	{ 
				 int  result = random.nextInt() % b; 
				 if  (result >= a)
				 { 
					 return  result; 
				 } 
		 	}
	}

	// dong ho tinh thoi gian
	public void time()
	{
		miligiay++;
		if(miligiay>=25)
		{			
			giay--;
			if(giay<=0)
			{
				if(giay<=0)
					minutes--;				
				if(minutes<=0)
				{
					minutes=0;
				}
				if(minutes==0)
				{
					minutes=0;
					giay=0;
				}
				if(minutes>0)
					giay=60;
			}
			miligiay=0;
		}
	}
	
	public void update()
	{	
				
		updateInputGameScr();
		camera.update(Bee_Player.xBalloon, Bee_Player.yBalloon);
		map.update();
		Bee_Player.update();
	}

	 private void updateInputGameScr()
	 {		

		 //xu ly nhay
		if (CCanvas.instance.keyPressed[CCanvas.UP]) //up
		{ 
			rec1.vy=-3;
			line1.vy=-3;
		}
		else if (CCanvas.instance.keyPressed[CCanvas.DOWN]) //down
		{
			rec1.vy=3;
			line1.vy=3;   
		}else if (CCanvas.instance.keyPressed[CCanvas.LEFT]) //left
		{
			rec1.vx=-3;
			line1.vx=-3;   
		}else if (CCanvas.instance.keyPressed[CCanvas.RIGHT]) //right
		{
			line1.vx=3;
			rec1.vx=3; 
		}
		 
		 CCanvas.instance.clearKeyPressed();
	 }
	 
	int angle=0;
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		Common.mGraphics gg= new Common.mGraphics(g);
		g.setColor(0xffffff);
		gg.fillRect(0, 0, CCanvas.w, CCanvas.h);
		//draw background
		//g.setColor(0x00);
		camera.translate(g);
       map.paint(g);	
       Bee_Player.paint(g);
	}	
}
